package com.laboratory.servicecustomer.workBench.controller;

import com.Laboratory.workBench.pojo.booking;
import com.Laboratory.workBench.pojo.room;
import com.Laboratory.workBench.service.roomService;
import com.laboratory.servicecustomer.constant.constantArgs;
import com.laboratory.servicecustomer.utilPojo.bookingTimePojo;
import com.laboratory.servicecustomer.utilPojo.comm;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author acok
 * @description:TODO
 * @Date 2022/12/16 14:52
 * @Version 1.0
 */
@Controller
@RequestMapping("/room")
public class roomController {
    @DubboReference(interfaceClass = roomService.class,version = "1.0.0")
    private roomService roomService;

          /**
               * @return: 获取教室实例列表
               * @param: 预约时间实例（预约日期、预约节次）
               * @description: 根据前台选定预约时间筛选符合条件的教室予以展示
               * @Author: Acok
               * @mbggenerated 2022/12/29
               */
    @RequiresRoles(value = {"users","admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"users:booking","admin:booking","Root:highest"},logical = Logical.OR)
    @PostMapping("/getRoomListByTime")
    @ResponseBody
    public Object getRoomListByTime(@RequestBody bookingTimePojo bookingTimePojo){
        comm comm=new comm();
        try{
            List<room> rooms= roomService.selectRoomListByTime(
                    bookingTimePojo.getDate(),
                    bookingTimePojo.getDuration()
            );
            comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
            comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            comm.setOther(rooms);
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

          /**
               * @return: 前后端响应对象
               * @param: 教室预约实例对象
               * @description: 根据前台传递预约相关参数新建预约记录
               * @Author: Acok
               * @mbggenerated 2022/12/29
               */
    @RequiresRoles(value = {"users","admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"users:booking","admin:booking","Root:highest"},logical = Logical.OR)
    @PostMapping("/insertBookingRecord")
    @ResponseBody
    public Object insertBookingRoomRecord(@RequestBody booking booking){
        comm comm =new comm();
        try{
            int i= roomService.insertBookingRoomRecord(booking);
            if (i>0){
                comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
                comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            }
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

          /**
               * @return: 房间的主键、房号实例对象集合
               * @param: 无参数
               * @description: 获取所有房间的主键、房间号供前台进行选择
               * @Author: Acok
               * @mbggenerated 2022/12/29
               */
    @RequiresRoles(value = {"users","admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"users:equipmentSearch","admin:equipmentSearch","Root:highest"},logical = Logical.OR)
    @GetMapping("/getRoomNoList")
    @ResponseBody
    public Object getRoomNoList(){
        comm comm=new comm();
        try{
            List<Map<Object,Object>> roomNoList = roomService.getRoomMsgList();
            comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
            comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            comm.setOther(roomNoList);
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

          /**
               * @return: 存在预约记录的房间的实例
               * @param: 分页参数
               * @description: 根据分页条件获取房间预约记录供用户确认预约记录
               * @Author: Acok
               * @mbggenerated 2022/12/29
               */
    @RequiresRoles(value = {"users","admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"users:bookingStatistics","admin:bookingStatistics","Root:highest"},logical = Logical.OR)
    @GetMapping("/getBookingRoomList")
    @ResponseBody
    public Object getBookingRoomList(@RequestParam("pageSize")Integer pageSize,@RequestParam("pageNo")Integer pageNo){
        comm comm=new comm();
        try{
            List<booking> roomBookingList = roomService.getRoomBookingList((pageNo-1)*pageSize,pageSize);
            comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
            comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            comm.setOther(roomBookingList);
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }
}
